package com.intermancer.predictor.evaluator;

import java.util.ArrayList;
import java.util.List;

import com.intermancer.predictor.data.ConsumeResponse;
import com.intermancer.predictor.data.Quantum;

public abstract class AbstractChannelEvaluator implements Evaluator {
	
	public static final int DEFAULT_CHANNEL_OFFSET = -1;
	public static final int DEFAULT_LOOK_AHEAD = 1;

	private int channelOffset;
	private int lookAhead;
	private double score;
	private List<Double> values;
	
	public AbstractChannelEvaluator() {
		this(DEFAULT_CHANNEL_OFFSET, DEFAULT_LOOK_AHEAD);
	}
	
	public AbstractChannelEvaluator(int channelOffset, int lookAhead) {
		setChannelOffset(channelOffset);
		setLookAhead(lookAhead);
	}

	public boolean handle(ConsumeResponse consumeResponse,
			Quantum quantum) {
		if(ConsumeResponse.CONSUME_COMPLETE.equals(consumeResponse)) {
			values.add(quantum.getChannel(channelOffset).getValue());
			if (values.size() > lookAhead) {
				score += compare(values.get(0).doubleValue(),
						quantum.getLastChannel().getValue().doubleValue());
				values.remove(0);
			}
		}
		return true;
	}

	public abstract double compare(double expected, double given);

	public int getChannelOffset() {
		return channelOffset;
	}

	public void setChannelOffset(int channelOffset) {
		this.channelOffset = channelOffset;
	}

	public int getLookAhead() {
		return lookAhead;
	}

	public void setLookAhead(int lookAhead) {
		this.lookAhead = lookAhead;
	}

	public void init() {
		score = 0.0;
		values = new ArrayList<Double>(lookAhead + 1);
	}
	
	public double getScore() {
		return score;
	}

}
